<!DOCTYPE html>
<html lang="en">
<head>
    
    <meta charset="utf-8">
    <title>layer/LayerUtil.js - sss</title>
    
    <meta name="description" content="aaaa" />
    
        <meta name="keywords" content="cccc" />
        <meta name="keyword" content="cccc" />
    
    
    
    <meta property="og:title" content="arcgis4.x 简单的操作工具"/>
    <meta property="og:type" content="website"/>
    <meta property="og:image" content=""/>
    <meta property="og:site_name" content="CPC"/>
    <meta property="og:url" content="https://gitee.com/CPC1994/arcgis4.X"/>
    
    <script src="scripts/prettify/prettify.js"></script>
    <script src="scripts/prettify/lang-css.js"></script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>

<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
  <div class="navicon"></div>
</label>

<label for="nav-trigger" class="overlay"></label>

<nav class="wrap">
    
    <input type="text" id="nav-search" placeholder="Search" />
    
    <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="EchartLayer.html">EchartLayer</a><ul class='methods'><li data-type='method' style='display: none;'><a href="EchartLayer.html#init">init</a></li><li data-type='method' style='display: none;'><a href="EchartLayer.html#setBaseMap">setBaseMap</a></li><li data-type='method' style='display: none;'><a href="EchartLayer.html#setChartOption">setChartOption</a></li><li data-type='method' style='display: none;'><a href="EchartLayer.html#setVisible">setVisible</a></li><li data-type='method' style='display: none;'><a href="EchartLayer.html#refreshBegin">refreshBegin</a></li><li data-type='method' style='display: none;'><a href="EchartLayer.html#refreshing">refreshing</a></li><li data-type='method' style='display: none;'><a href="EchartLayer.html#refreshEnd">refreshEnd</a></li><li data-type='method' style='display: none;'><a href="EchartLayer.html#on">on</a></li><li data-type='method' style='display: none;'><a href="EchartLayer.html#off">off</a></li><li data-type='method' style='display: none;'><a href="EchartLayer.html#setCharts">setCharts</a></li><li data-type='method' style='display: none;'><a href="EchartLayer.html#createLayer">createLayer</a></li><li data-type='method' style='display: none;'><a href="EchartLayer.html#removeLayer">removeLayer</a></li><li data-type='method' style='display: none;'><a href="EchartLayer.html#startMapEventListeners">startMapEventListeners</a></li><li data-type='method' style='display: none;'><a href="EchartLayer.html#getE3CoordinateSystem">getE3CoordinateSystem</a></li></ul></li><li></li><li><a href="LayerUtil.html">LayerUtil</a><ul class='methods'><li data-type='method' style='display: none;'><a href="LayerUtil.html#lazyLoadingLayer">lazyLoadingLayer</a></li><li data-type='method' style='display: none;'><a href="LayerUtil.html#applyLayer">applyLayer</a></li><li data-type='method' style='display: none;'><a href="LayerUtil.html#applyMapImageLayer">applyMapImageLayer</a></li><li data-type='method' style='display: none;'><a href="LayerUtil.html#applyTileLayer">applyTileLayer</a></li><li data-type='method' style='display: none;'><a href="LayerUtil.html#applyFeatureLayer">applyFeatureLayer</a></li><li data-type='method' style='display: none;'><a href="LayerUtil.html#applySceneLayer">applySceneLayer</a></li><li data-type='method' style='display: none;'><a href="LayerUtil.html#applyElevationLayer">applyElevationLayer</a></li><li data-type='method' style='display: none;'><a href="LayerUtil.html#switchLayer">switchLayer</a></li><li data-type='method' style='display: none;'><a href="LayerUtil.html#getLayerByUniqueId">getLayerByUniqueId</a></li></ul></li><li></li><li><a href="ViewUtil.html">ViewUtil</a><ul class='methods'><li data-type='method' style='display: none;'><a href="ViewUtil.html#flyToPosition3D">flyToPosition3D</a></li><li data-type='method' style='display: none;'><a href="ViewUtil.html#flyToPoint2D">flyToPoint2D</a></li><li data-type='method' style='display: none;'><a href="ViewUtil.html#applyMapView">applyMapView</a></li><li data-type='method' style='display: none;'><a href="ViewUtil.html#removeMapView">removeMapView</a></li><li data-type='method' style='display: none;'><a href="ViewUtil.html#applySceneView">applySceneView</a></li><li data-type='method' style='display: none;'><a href="ViewUtil.html#removeSceneView">removeSceneView</a></li><li data-type='method' style='display: none;'><a href="ViewUtil.html#getMapStatus">getMapStatus</a></li><li data-type='method' style='display: none;'><a href="ViewUtil.html#getMap">getMap</a></li><li data-type='method' style='display: none;'><a href="ViewUtil.html#getMapViewMap">getMapViewMap</a></li><li data-type='method' style='display: none;'><a href="ViewUtil.html#getSceneViewMap">getSceneViewMap</a></li><li data-type='method' style='display: none;'><a href="ViewUtil.html#getView">getView</a></li><li data-type='method' style='display: none;'><a href="ViewUtil.html#getMapView">getMapView</a></li><li data-type='method' style='display: none;'><a href="ViewUtil.html#getSceneView">getSceneView</a></li><li data-type='method' style='display: none;'><a href="ViewUtil.html#bind2D_3D">bind2D_3D</a></li><li data-type='method' style='display: none;'><a href="ViewUtil.html#switchView">switchView</a></li><li data-type='method' style='display: none;'><a href="ViewUtil.html#addMapSwitchEvent">addMapSwitchEvent</a></li><li data-type='method' style='display: none;'><a href="ViewUtil.html#removeMapSwitchEvent">removeMapSwitchEvent</a></li></ul></li><li></li><li><a href="OverViewUtil.html">OverViewUtil</a><ul class='methods'><li data-type='method' style='display: none;'><a href="OverViewUtil.html#updateOverview">updateOverview</a></li><li data-type='method' style='display: none;'><a href="OverViewUtil.html#updateOverviewExtent">updateOverviewExtent</a></li><li data-type='method' style='display: none;'><a href="OverViewUtil.html#remove">remove</a></li><li data-type='method' style='display: none;'><a href="OverViewUtil.html#switchOverview">switchOverview</a></li><li data-type='method' style='display: none;'><a href="OverViewUtil.html#getOverViewMapId">getOverViewMapId</a></li></ul></li><li></li><li><a href="ScaleBarUtil.html">ScaleBarUtil</a><ul class='methods'><li data-type='method' style='display: none;'><a href="ScaleBarUtil.html#defaultStyle">defaultStyle</a></li><li data-type='method' style='display: none;'><a href="ScaleBarUtil.html#remove">remove</a></li></ul></li></ul><h3>Modules</h3><ul><li><a href="module-arcgisTools.html">arcgisTools</a><ul class='methods'><li data-type='method' style='display: none;'><a href="module-arcgisTools.html#~loadAPI">loadAPI</a></li></ul></li><li><a href="module-layer_layerManage.html">layer/layerManage</a><ul class='methods'><li data-type='method' style='display: none;'><a href="module-layer_layerManage.html#~switchLayers">switchLayers</a></li><li data-type='method' style='display: none;'><a href="module-layer_layerManage.html#~switchLayer">switchLayer</a></li><li data-type='method' style='display: none;'><a href="module-layer_layerManage.html#~lazyLoadingLayer">lazyLoadingLayer</a></li><li data-type='method' style='display: none;'><a href="module-layer_layerManage.html#~applyLayerByUrl">applyLayerByUrl</a></li><li data-type='method' style='display: none;'><a href="module-layer_layerManage.html#~applyLayerByConfig">applyLayerByConfig</a></li><li data-type='method' style='display: none;'><a href="module-layer_layerManage.html#~applyMapImageLayerByUrl">applyMapImageLayerByUrl</a></li><li data-type='method' style='display: none;'><a href="module-layer_layerManage.html#~applyMapImageLayerByConfig">applyMapImageLayerByConfig</a></li><li data-type='method' style='display: none;'><a href="module-layer_layerManage.html#~applyTileLayerByUrl">applyTileLayerByUrl</a></li><li data-type='method' style='display: none;'><a href="module-layer_layerManage.html#~applyTileLayerByConfig">applyTileLayerByConfig</a></li><li data-type='method' style='display: none;'><a href="module-layer_layerManage.html#~applyElevationLayerByUrl">applyElevationLayerByUrl</a></li><li data-type='method' style='display: none;'><a href="module-layer_layerManage.html#~applyElevationLayerByConfig">applyElevationLayerByConfig</a></li></ul></li><li><a href="module-view_viewManage.html">view/viewManage</a><ul class='methods'><li data-type='method' style='display: none;'><a href="module-view_viewManage.html#~flyToPosition3D">flyToPosition3D</a></li><li data-type='method' style='display: none;'><a href="module-view_viewManage.html#~flyToPoint2D">flyToPoint2D</a></li><li data-type='method' style='display: none;'><a href="module-view_viewManage.html#~applyMapView">applyMapView</a></li><li data-type='method' style='display: none;'><a href="module-view_viewManage.html#~removeMapView">removeMapView</a></li><li data-type='method' style='display: none;'><a href="module-view_viewManage.html#~removeSceneView">removeSceneView</a></li><li data-type='method' style='display: none;'><a href="module-view_viewManage.html#~applySceneView">applySceneView</a></li><li data-type='method' style='display: none;'><a href="module-view_viewManage.html#~getMapStatus">getMapStatus</a></li><li data-type='method' style='display: none;'><a href="module-view_viewManage.html#~getMap">getMap</a></li><li data-type='method' style='display: none;'><a href="module-view_viewManage.html#~getMapViewMap">getMapViewMap</a></li><li data-type='method' style='display: none;'><a href="module-view_viewManage.html#~getSceneViewMap">getSceneViewMap</a></li><li data-type='method' style='display: none;'><a href="module-view_viewManage.html#~getView">getView</a></li><li data-type='method' style='display: none;'><a href="module-view_viewManage.html#~getMapView">getMapView</a></li><li data-type='method' style='display: none;'><a href="module-view_viewManage.html#~getSceneView">getSceneView</a></li><li data-type='method' style='display: none;'><a href="module-view_viewManage.html#~bind2D_3D">bind2D_3D</a></li><li data-type='method' style='display: none;'><a href="module-view_viewManage.html#~switchView">switchView</a></li><li data-type='method' style='display: none;'><a href="module-view_viewManage.html#~addMapSwitchEvent">addMapSwitchEvent</a></li><li data-type='method' style='display: none;'><a href="module-view_viewManage.html#~removeMapSwitchEvent">removeMapSwitchEvent</a></li></ul></li><li><a href="module-widgets_widgetsManage.html">widgets/widgetsManage</a><ul class='methods'><li data-type='method' style='display: none;'><a href="module-widgets_widgetsManage.html#~addScaleBar">addScaleBar</a></li><li data-type='method' style='display: none;'><a href="module-widgets_widgetsManage.html#~removeScaleBar">removeScaleBar</a></li><li data-type='method' style='display: none;'><a href="module-widgets_widgetsManage.html#~addOverview">addOverview</a></li><li data-type='method' style='display: none;'><a href="module-widgets_widgetsManage.html#~getOverViewMapId">getOverViewMapId</a></li><li data-type='method' style='display: none;'><a href="module-widgets_widgetsManage.html#~removeOverview">removeOverview</a></li><li data-type='method' style='display: none;'><a href="module-widgets_widgetsManage.html#~switchOverview">switchOverview</a></li></ul></li></ul>
</nav>

<div id="main">
    
    <h1 class="page-title">layer/LayerUtil.js</h1>
    

    



    
    <section>
        <article>
            <pre class="prettyprint source linenums"><code>import {esri} from "../api/esriAPI";

/**
 * 图层逻辑操作-----------业务开发中，不允许直接调用它，如果需要，需要通过工具中向外提供接口
 * @class
 * */
export  const  LayerUtil = class{
  _uniqueIdMapConfig = new Map();//uniqueId和配置项的映射-----------理解为所有的图层的配置项
  _uniqueIdMapLayer = new Map();//uniqueId和layer的映射-------------理解为已加载的图层
  /**
   * 构造函数
   */
  constructor() {
  }

  /**
   * 懒加载图层的配置项，为了更高的性能，为后期配置化，做准备
   * @param {String} map  arcgis的Map对象
   * @param {Array} configs  配置项的数组
   */
  lazyLoadingLayer(map, configs) {
    for (let config of configs) {
      let {visible, uniqueType, uniqueId, sublayers} = config;
      //图层：visible:true，或者不设置visible的话----直接加载
      if (typeof visible == "undefined" || visible) {
        let layer = this.applyLayer(config);
        map.add(layer);
      } else {//父图层的visible:false的情况----实行懒加载
        //是MapImageLayer类型，同时配置了sublayers
        if (uniqueType == "MapImageLayer" &amp;&amp; sublayers) {
          for (let sublayer of sublayers) {
            //针对MapImageLayer类型，如果父图层是隐藏的状态，那么所有的子图层都将被改成隐藏状态
            sublayer.visible = visible &amp;&amp; sublayer.visible;
            if (this._uniqueIdMapConfig.has(sublayer.uniqueId)) {
              console.error("存在该配置项了，请检查是否有误", sublayer.uniqueId);
            } else {
              //为了节约存储，只保存父图层的映射索引
              this._uniqueIdMapConfig.set(sublayer.uniqueId, uniqueId);
            }
          }
        }
        if (this._uniqueIdMapConfig.has(uniqueId)) {
          console.error("存在该配置项了，请检查是否有误", uniqueId);
        } else {
          //保存图层的映射索引
          this._uniqueIdMapConfig.set(uniqueId, config);
        }
      }
    }
  }

  /**
   * 加载各种类型的图层 ---------------- 不够需要自己扩展
   * @param {Object} config  配置项
   * @returns {Object} arcgis图层对象
   */
  applyLayer(config) {
    let {uniqueId, uniqueType} = config;
    //必须要有uniqueId
    if (!uniqueId) {
      console.error("没有配置uniqueId，不允许执行", uniqueId)
      return;
    }
    //判断是否支持该类型的加载
    if (!this["apply" + uniqueType]) {
      console.error("暂时不支持该类型的加载，如果需要自己扩展apply方法", uniqueType);
      return;
    }
    //加载不同类型的图层----例如：apply + TileLayer   =====>  applyTileLayer
    return this["apply" + uniqueType](config);
  }

  /***************************************各类图层的加载 **********************************************************/

  /**
   * 申请MapImageLayer图层
   * @param {Object} config  配置项
   * @returns {Object} arcgis图层对象
   */
  applyMapImageLayer(config) {
    let uniqueId = config.uniqueId;
    if (this._uniqueIdMapLayer.has(uniqueId)) {
      console.error("已存在该图层，无法重新创建，请更改uniqueId,返回了已存在的图层", uniqueId);
      return this._uniqueIdMapLayer.get(uniqueId);
    }
    config.uniqueType = "MapImageLayer";
    let layer = new esri.MapImageLayer(config);
    var items = layer.allSublayers.items;//若没有配置sublayer，这里由于时机问题，将为undefine，满足我们要的情况
    for (let sublayer of items) {
      this._uniqueIdMapLayer.set(sublayer.uniqueId, sublayer);
    }
    this._uniqueIdMapLayer.set(uniqueId, layer);
    return layer;
  }

  /**
   * 申请TileLayer图层
   * @param {Object} config 申请TileLayer的配置项.
   * @returns {Object} arcgis图层对象   如果该图层对象已被记录了，将提示报错信息
   */
  applyTileLayer(config) {
    let uniqueId = config.uniqueId;
    if (this._uniqueIdMapLayer.has(uniqueId)) {
      console.error("已存在该图层，无法重新创建，请更改uniqueId,返回了已存在的图层", uniqueId);
      return this._uniqueIdMapLayer.get(uniqueId);
    }
    config.uniqueType = "TileLayer";
    let layer = new esri.TileLayer(config);
    this._uniqueIdMapLayer.set(uniqueId, layer);
    return layer;
  }

  /**
   * 申请FeatureLayer图层
   * @param {Object} config 申请FeatureLayer的配置项.
   * @returns {Object} arcgis图层对象   如果该图层对象已被记录了，将提示报错信息
   */
  applyFeatureLayer(config) {
    let uniqueId = config.uniqueId;
    if (this._uniqueIdMapLayer.has(uniqueId)) {
      console.error("已存在该图层，无法重新创建，请更改uniqueId,返回了已存在的图层", uniqueId);
      return this._uniqueIdMapLayer.get(uniqueId);
    }
    config.uniqueType = "FeatureLayer";
    let layer = new esri.FeatureLayer(config);
    this._uniqueIdMapLayer.set(uniqueId, layer);
    return layer;
  }

  /**
   * 申请SceneLayer图层
   * @param {Object} config 申请SceneLayer的配置项.
   * @returns {Object} arcgis图层对象   如果该图层对象已被记录了，将提示报错信息
   */
  applySceneLayer(config) {
    let uniqueId = config.uniqueId;
    if (this._uniqueIdMapLayer.has(uniqueId)) {
      console.error("已存在该图层，无法重新创建，请更改uniqueId,返回了已存在的图层", uniqueId);
      return this._uniqueIdMapLayer.get(uniqueId);
    }
    config.uniqueType = "SceneLayer";
    let layer = new esri.SceneLayer(config);
    this._uniqueIdMapLayer.set(uniqueId, layer);
    return layer;
  }
  /**
   * 申请ElevationLayer图层 -------------高程
   * @param {Object} config 申请ElevationLayer的配置项.
   * @returns {Object} arcgis图层对象   如果该图层对象已被记录了，将提示报错信息
   */
  applyElevationLayer(config) {
    let uniqueId = config.uniqueId;
    if (this._uniqueIdMapLayer.has(uniqueId)) {
      console.error("已存在该图层，无法重新创建，请更改uniqueId,返回了已存在的图层", uniqueId);
      return this._uniqueIdMapLayer.get(uniqueId);
    }
    config.uniqueType = "ElevationLayer";
    let layer = new esri.ElevationLayer(config);
    this._uniqueIdMapLayer.set(uniqueId, layer);
    return layer;
  }


  /***************************************对图层的各类操作 ********************************************************/
  /**
   * 图层切换
   * @param {String} map  arcgis的Map对象
   * @param {String} uniqueId  唯一识别的id
   * @param {Boolean} visible  是否显示
   */
  switchLayer(map, uniqueId, visible) {
    if (this._uniqueIdMapLayer.has(uniqueId)) {
      let layer = this._uniqueIdMapLayer.get(uniqueId);
      layer.visible = visible
      return;
    }
    //如果该图层是隐藏，同时没有加载，处于配置项的状态，将不做处理
    if (visible) {
      let layer = this.getLayerByUniqueId(map,uniqueId);
      layer &amp;&amp; (layer.visible = true);
    }
  }

  /**
   * 根据uniqueId获得对应的Layer对象，如果没有加载，但是存在于配置项中，会根据配置项加载该图层，并返回
   * @param {String} map  arcgis的Map对象
   * @param {String} uniqueId  唯一识别的id
   */
  getLayerByUniqueId(map,uniqueId) {
    let layer = null;
    if (this._uniqueIdMapLayer.has(uniqueId)) {
      layer = this._uniqueIdMapLayer.get(uniqueId);
      return layer;
    }
    if (!this._uniqueIdMapConfig.has(uniqueId)) {
      console.error("获得图层（"+uniqueId+"）出错了，不存在该配置项");
      return;
    }
    let config = this._uniqueIdMapConfig.get(uniqueId);
    //如果存在的配置项，只是父图层的索引，将直接获得父图层的配置项
    if (typeof config == "string") {
      config = this._uniqueIdMapConfig.get(config);
    }
    config.visible = true;
    layer = this.applyLayer(config);
    map &amp;&amp; map.add(layer)//将图层添加到地图中
    layer = this._uniqueIdMapLayer.get(uniqueId);//避免MapImageLayer得到的不是对应的子图层
    layer.visible = true
    return layer;
  }



}
</code></pre>
        </article>
    </section>




    
    
</div>

<br class="clear">

<footer>
    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Sat May 11 2019 22:54:19 GMT+0800 (GMT+08:00) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
<script src="scripts/polyfill.js"></script>
<script src="scripts/linenumber.js"></script>

<script src="scripts/search.js" defer></script>


<script src="scripts/collapse.js" defer></script>


</body>
</html>
